<?php
namespace app\admin\controller\sum\store;

use addons\sum\library\pay\PayService;
use addons\sum\service\pay\PayOper;
use addons\sum\service\store\Agent as StroeService;
use addons\sum\service\user\Coupon as CouponService;
use addons\sum\service\Wallet as WalletService;
use app\admin\model\sum\Pay;
use app\admin\model\sum\user\User as UserModel;
use think\Db;
use think\exception\HttpResponseException;
use addons\sum\exception\SumException;
use app\admin\model\sum\order\Order as OrderModel;
use app\admin\model\sum\goods\Goods as GoodsModel;
use app\admin\model\sum\store\Goods as StoreGoodsModel;

/**
 * 商户订单管理
 */
class Order extends \app\admin\controller\sum\Common
{
    public function _initialize()
    {
        \app\admin\controller\sum\Common::_initialize();
        $this->model = new OrderModel;
    }
    /**
     * 商户列表
     */
    public function index()
    {
        $store_id = $this->request->param('store_id');
        if(!$store_id){
            $this->error('参数错误');
        }
        if (!$this->auth->isSuperAdmin()) {
            $admin=$this->auth->getUserInfo();
            if($admin['store_id']!=$store_id){
                $this->error('你不能操作该商户订单');
            }
        }

        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }
        $records = $this->model->sheepFilter()->with(['items'])->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $records);
    }


    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        if (!$detail) {
            $this->error(__('No Results were found'));
        }
        $detail['dian']=json_decode($detail['dian'],true);
        $this->success('获取成功', null, $detail);
    }


    /**
     * 删除(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list) {
            $count = 0;
            foreach ($list as $item) {
                $count += $item->delete();
            }
            return $count;
        });

        if ($result) {
            $this->success('删除成功', null, $result);
        } else {
            $this->error(__('No rows were deleted'));
        }
    }

    /**
     * 核销
     * @param  $id
     * @return \think\Response
     */
    public function hexiao($id)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list) {
            $count = 0;
            foreach ($list as $item) {
                $params['id'] = $item->id;
                $params['status']='completed';
                $count += $item->save($params);

                $staff=UserModel::where('id',$item['staff_id'])->find();
                if($staff){
                    WalletService::change($staff, 'commission', $item['pay_fee'], 'reward_income', '员工业绩');
                }
                $res=CouponService::shopping_give($item['user_id']);//赠送优惠劵
            }
            return $count;
        });

        if ($result) {
            $this->success('操作成功', null, $result);
        } else {
            $this->error(__('操作失败，未改变任何记录'));
        }
    }



}
